<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { productTypeArr, goodsClass } from '@/pinia/getter';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import xmImage from '@/components/xmImage/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import goodsForm from './goodsForm.vue';
  import goodsPrice from './goodsPrice.vue';
import { ElMessageBox } from 'element-plus';
  const goodsFormRef = ref(),
        goodsPriceRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page: 1,
    limit: 10,
    goods_type: 0,
    sort: "+id",
    gift_id: "",
    gift_name: "",
    mall_goods_class: -1,
    level: -1,
  })
  const handleSerach = () => {
  listQuery.page = 1;
  getList();
};
const resetQuery = () => {
  Object.assign(listQuery, {
    page: 1,
    limit: 10,
    goods_type: 0,
    sort: "+id",
    gift_id: "",
    gift_name: "",
    mall_goods_class: null,
    level: null,
  });
};
  const handleForm = (row:any = {})=>{
    goodsFormRef.value.openDialog({
      ...row,
      goodsClass
    })
  }
  const handleDel = (row:any={})=>{
    const { goods_name='', goods_id } = row
    ElMessageBox.confirm(`确定删除商品：${goods_name}的此价格吗？`, '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      const loading = Toastloading()
      propApi.deleteGood(goods_id).then(()=>{
        ToastSuccess('删除成功')
        getList()
        loading.close()
      }).catch(()=>{
        loading.close()
      })
    }).catch(() => { })
  }
  const goodsTypeFilters = (type:number = -1)=>{
    const { label='其他' } = productTypeArr.find(res=>res.value == type) || { label:'其他' }
    return label
  }
  /**
   * 商品价格 0新增 1修改 2设为默认价格
   */
  const handlePrice = (row:any={}, skuItem:any={},type:number=0)=>{
    const { goods_name='', goods_id } = row
    if(!type || type == 1) {
      goodsPriceRef.value.openDialog({
        goods_name,
        goods_id,
        ...skuItem
      })
    }
    if(type == 2) {
      const { sku_id='' } = skuItem
      if(!sku_id) return ToastWarning('缺少sku_id')
      ElMessageBox.confirm(`确定设置此价格为商品：${goods_name}的默认价格吗？`, '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        const loading = Toastloading()
        propApi.defaultGoodSku(sku_id).then(()=>{
          ToastSuccess('设置默认价格成功')
          getList()
          loading.close()
        }).catch(()=>{
          loading.close()
        })
      }).catch(() => { })
    }
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await propApi.getGoods(listQuery)
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">商品ID：</div>
          <el-input v-model="listQuery.gift_id" placeholder="请输入商品ID"/>
        </div>
        <div class="labelItem">
          <div class="label">商品名称：</div>
          <el-input v-model="listQuery.gift_name" placeholder="请输入商品名称"/>
        </div>
        <div class="labelItem">
          <div class="label">商品类型：</div>
          <el-select v-model="listQuery.goods_type" placeholder="请选择商品类型">
            <el-option label="全部类型" :value="0" />
            <el-option
              v-for="item in productTypeArr"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">商品分类：</div>
          <el-select v-model="listQuery.mall_goods_class" placeholder="请选择商品分类">
            <el-option label="全部分类" :value="-1" />
            <el-option
              v-for="item in goodsClass"
              :label="item.label"
              :value="item.value" />
          </el-select>
        </div>
      </div>
      <div class="labelBox">
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleForm()">新增商品</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="goods_id" label="商品ID" />
      <el-table-column width="180" prop="goods_name" label="商品名称" />
      <el-table-column width="120" prop="goods_type" label="商品类型">
        <template #default="scoped">
          {{ goodsTypeFilters(scoped.row.goods_type) }}
        </template>
      </el-table-column>
      <el-table-column width="120" prop="mall_goods_class" label="商品分类">
        <template #default="scoped">
          {{ goodsClass.find(res=>res.value == scoped.row.mall_goods_class)?.label || ''  }}
        </template>
      </el-table-column>
      <el-table-column width="140" prop="big_img" label="商品图片">
        <template #default="scoped">
          <xmImage :src="scoped.row.big_img" :list="[scoped.row.big_img]" />
        </template>
      </el-table-column>
      <el-table-column width="100" prop="is_show" label="是否显示">
        <template #default="scoped">
          <div>
            <el-icon>
              <SuccessFilled v-if="scoped.row.is_show == 1" style="color: #67C23A;" />
              <CircleCloseFilled v-if="scoped.row.is_show == 0" style="clear: #F56C6C;" />
            </el-icon>
          </div>
        </template>
      </el-table-column>
      <el-table-column width="110" sortable prop="sort" label="商品排序" />
      <el-table-column min-width="340" prop="price" label="价格" >
        <template #default="scoped">
          <div
              v-for="(item, key) in scoped.row.mall_goods_sku"
              :key="key">
            <el-tag>
              {{ item.time_limit }}天 {{ item.price }}钻石
              <!-- {{ ['非默认','默认'][item.default]  }} -->
              <span
                style="color: blue;"
                class="pointer"
                @click="handlePrice(scoped.row,item,1)">编辑</span>&nbsp;
              <span
                v-if="!item.default"
                style="color: rgb(31, 139, 23);"
                class="pointer"
                @click="handlePrice(scoped.row,item,2)">设为默认价格</span>
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column width="200" show-overflow-tooltip prop="goods_desc" label="商品描述" />
      <el-table-column fixed="right" width="300"  label="操作" >
        <template #default="scoped">
          <el-button type="primary" @click="handlePrice(scoped.row)">新增价格</el-button>
          <el-button type="primary" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
      <goodsForm ref="goodsFormRef" @update-list="getList" />
      <goodsPrice ref="goodsPriceRef" @update-list="getList" />
  </div>
</template>